package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class PersonContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonContainer
{
    private ArrayList<Person> employees;
    private ArrayList<Person> customers;
    private ArrayList<Person> suppliers;
    private static PersonContainer instance;

    /**
     * Constructor for object of class PersonContainer.
     */
    public PersonContainer()
    {
        employees = new ArrayList<Person>();
        customers = new ArrayList<Person>();
        suppliers = new ArrayList<Person>();
    }

    /**
     * Singleton, so only one instance of OrderContainer can be made.
     */
    public static PersonContainer getInstance()
    {
        if(instance == null)
        {
            instance = new PersonContainer();
        }
        return instance;
    }

    /**
     * Searches for a customer by searching on the customers phonenumber, and returns that customer.
     * @param phoneNumber, the phonenumber on the customer to be found.
     * @param customerId, the id on the customer to be found. (not used but was the intention to begin with).
     * @return customers.get(index), the found customer to be reurned.
     */
    public Person findCustomer(String phoneNumber, int customerId)
    {
        int index = 0;
        boolean found = false;
        while(!found && index < customers.size())

            if(customers.get(index).getPhoneNumber().equals(phoneNumber))
            {
                found = true;
            } 
            else
            {
                index++;
        }

        return customers.get(index);
    }

    /**
     * Logs user on, validates by comparing username and password input to those stored.
     * @param userName, the user to logged on.
     * @param password, the password to be validated.
     * @return employees.get(index), the employee returned.
     */
    public Person logOn(String userName, String password)
    {
        int index = 0;
        boolean found = false;
        while(!found && index < employees.size())
            if(employees.get(index).getUserName().equals(userName) && (employees.get(index).getPassword().equals(password)))
            {
                found = true;   
            }
            else
            {
                index++;
        }

        return employees.get(index);
    }

    /**
     * Adds an employee to the personcontainer and sets an id for this employee.
     * @param employee, the employee to be added an id assigned to.
     */
    public void addEmployee(Person employee)
    {
        employees.add(employee);
        int id = 10000;
        for(Person employee2 : employees)
            if(employee2.getId() > id)
            {
                id = employee2.getId();
        }
        id++;
        employee.setId(id);
    }

    /**
     * Adds a customer to the personcontainer and sets an id for this customer.
     * @param customer, the customer to be added an id assigned to.
     */
    public void addCustomer(Person customer)
    {
        customers.add(customer);
        int id = 10000;
        for(Person customer2 : customers)
            if(customer2.getId() > id)
            {
                id = customer2.getId();
        }
        id++;
        customer.setId(id);
    }
}

